
package com.dsiguero.tfg.percentdroid.drawer.list.item;

import com.dsiguero.tfg.percentdroid.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class EntryAdapter extends ArrayAdapter<Item> {

    private Context context;

    private ArrayList<Item> items;

    private LayoutInflater vi;

    public EntryAdapter(Context context, ArrayList<Item> items) {
        super(context, 0, items);
        this.context = context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        final Item i = items.get(position);
        if (i != null) {
            switch (i.getType()) {
                case Item.ITEM_HEADER:
                    HeaderItem hi = (HeaderItem)i;
                    v = vi.inflate(R.layout.drawer_list_header, null);

                    v.setOnClickListener(null);
                    v.setOnLongClickListener(null);
                    v.setLongClickable(false);

                    final TextView sectionView = (TextView)v.findViewById(R.id.drawer_header_title);
                    sectionView.setText(hi.getTitle());

                    break;
                case Item.ITEM_NORMAL:
                    // Rellena el TextView con el texto que hubiera en itemView
                    // y con la imagen
                    EntryItem ei = (EntryItem)i;
                    v = vi.inflate(R.layout.drawer_list_item, null);

                    final TextView itemTextView = (TextView)v
                            .findViewById(R.id.list_item_entry_title);
                    final ImageView itemImageView = (ImageView)v
                            .findViewById(R.id.list_item_entry_drawable);

                    itemTextView.setText(ei.getTitle());
                    itemImageView.setImageResource(ei.getImgId());
                    // IMPLEMENTAR AQUI EL ONCLICK??

                    break;

            }
        }
        return v;
    }
}
